<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2017/12/22
 * Time: 14:58
 */

namespace App\Http\Logics\MiniProgram;

use App\Cafe;
use App\City;
use App\UserFollowCity;
use App\UserMappingCity;
use App\Users;
use Illuminate\Support\Facades\Redis;
use App\Http\Logics\BaseLogic;
class CityLogic extends BaseLogic
{

    //
    /**
     * 城市管理员所管理的城市详情
     * @param $data
     * @return mixed
     */
    public static function detail($data){
        try{
            $wx_app_openid = Redis::get($data['loginSession']);
            $user = Users::where('wx_app_openid',$wx_app_openid)->first();
            foreach ($user->user_citys()->get() as $item){
                    $item->pinyin = $item->name;
            }
            $user->cityManager = $user->user_citys()->get();
            return self::response(self::SUCCESS,$user);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    /**
     * 城市详情
     * @param $data
     * @return mixed
     */
    public static  function cityDetail($data){
        try{
            $city = City::withCount(['cafe' => function ($query){
                $query->where('status', Cafe::ONLINE);
            }, 'user'])->where(function ($query) use ($data){
                if (isset($data['citycode']) && $data['citycode'] != ''){
                    $query->where('code', $data['citycode']);
                }
                if (isset($data['city_id']) && $data['city_id']){
                    $query->where('id', $data['city_id']);
                }
            })->first();

            if (empty($city)){
                return self::response(self::CONTENT_IS_EMPTY);
            }

            // 判断用户是否登录，给城市加上用户的关注状态
            if (!empty($data['loginSession'])){
                $response = self::getUserIdByLoginSession($data['loginSession']);
                if ($response->code) {
                    return self::response($response->code);
                }
                $user_id = $response->data;

                $city->is_followed = UserFollowCity::where(function ($query) use ($city){
                        $query->where('city_id', $city->id);
                })->where('user_id', $user_id)->count() ? 1 : 0;

            }else{
                $city->is_followed = 0;
            }

            return self::response(self::SUCCESS, $city);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    public static function cityUpdate($data){
        try{
            $cityCode = $data['cityCode'];
            $summary = $data['summary'];
            $slogan = $data['slogan'];
            $status = $data['status'];
            $banner = $data['banner'];
            $city = City::where('code',$cityCode)->first();
            $city->summary = $summary != "null" ? $summary : "";
            $city->slogan = $slogan != "null" ? $slogan : "";
            $city->status = $status;
            $city->banner = $banner;
            if($city->save()){
                return self::response(self::SUCCESS);
            }else{
                return self::response(self::OBJECT_SAVE_FAIL);

            }
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    public static function getCityList($data)
    {
        $query = City::withCount(['cafe' => function ($query){
            $query->where('status', Cafe::ONLINE);
        }])->where(function ($query) use ($data){
            if ($data['status'] != ''){
                $query->where('status', $data['status']);
            }
            if (isset($data['name']) && !empty($data['name'])){
                $query->where('name', 'like', '%' . $data['name'] . '%');
            }
        })->orderBy('id', 'asc');
        $city = $data['limit'] ? $query->paginate($data['limit']) : $query->get();
        if (empty($city)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $city);
    }

    public static function setCityStatus($data)
    {
        $city = City::find($data['city_id']);
        if (empty($city)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        $city->status = !$city->status;
        if (!$city->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    public static function recommend($data)
    {
        $query = City::where(function ($query) use ($data){
            $query->where('status', $data['status']);
        })->where('is_recommend', 1);
        $city = $data['limit'] ? $query->paginate($data['limit']) : $query->get();
        if (empty($city)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $city);
    }

    public static function searchCity($data)
    {
        $query = City::where(function ($query) use ($data){
            $query->where('name', 'like', '%' . $data['name'] . '%');
        });
        $city = $data['limit'] ? $query->paginate($data['limit']) : $query->get();

        if (empty($city)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $city);
    }

    /**
     * 上传城市封面
     * @param $data
     */
    public static function uploadBanner($data)
    {
        try{
            $url = uploadImage(storage_path().'/app/'.$data['banner_file'], 'city-banner');
            return self::response(self::SUCCESS, $url);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    public static function setCityBaseInfo($data)
    {
        $city = City::where('code', $data['citycode'])->first();
        if (empty($city)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        $city->banner = $data['banner'];
        $city->slogan = $data['slogan'];
        if (!$city->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    public static function getCityBaseInfo($data)
    {
        $city = City::where('code', $data['citycode'])->first();
        if (empty($city)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $city);
    }

}